package cn.bone.mqtt.demo03;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;

/**
 * Created by MD-01 on 2017/4/24.
 */
public class Client02 {

    public static void main(String[] args) throws MqttException {
        //String host = "tcp://127.0.0.1:61613";
        //String host = "tcp://127.0.0.1:1883";
        String host = "tcp://172.16.252.234:1883";
        MqttClient client = new MqttClient(host,"clientId-02",
                new MqttDefaultFilePersistence("d:/mqtt"));
        MqttConnectOptions options = new MqttConnectOptions();
        options.setUserName("admin");
        options.setPassword("password".toCharArray());
        options.setCleanSession(false);
        //设置自动重新连接
        options.setAutomaticReconnect(true);
        options.setWill("client/disOnline","client02,disOnline".getBytes(),2,true);
        client.setCallback(new DemoCallBack());
        client.connect(options);
        client.subscribe("topic/client-02",2);
        client.subscribe("topic/all",2);
    }
}
